#!/bin/bash

CUR_DIR=$(pwd)

function lcov_isexits() {
    check_results=$(lcov -v | grep lcov)
    if [[ $check_results =~ "lcov" ]]; then
        return 0
    else
        echo "lcov can't be used. Install it"
        exit 1
    fi
}


function pilling() {
    sed -i '/NotifyProcessActive();/i__gcov_flush();' src/gausskernal/process/postmaster/postmaster.cpp
    sed -i 'N;256 i extern "C" void __gcov_flush();' src/gausskernel/process/postmaster/postmaster.cpp
}

function ispipilling_success() {
    check_result=$(git diff src/gausskernel/process/postmaster/postmaster.cpp | grep "+++")
    if [[ $check_result == "+++" ]]; then
        echo "Adding pile driving function succeeded"
    else
        echo "ERROR: Adding pile driving function failed"
    fi
}

function run_schedule() {
    make fastcheck_single PART=$1
}

function get_param() {
    case "$1" in
    -all)
        echo "---------Code Coverage ----------"
        lcov --capture --directory . --output-file coverage.info
        run_schedule $2
        ;;
    -add)
        #lcov xxxxxxxxxx
        ;;
    -h)
        usage
        ;;
    -init)
        git clean -dfx
        ./configure --gcc-version=7.3.0 CC=g++ CFLAGS='-O0 -fprofile-arcs -ftest-coverage' --prefix=$GAUSSHOME --3rd=$BINARYLIBS --enable-debug --enable-cassert --enable-thread-safety --with-readline --without-zlib LDFLAGS='-lgcov'
        pilling
        ;;
    *)
        usage
        exit 1
        ;;
    esac
}

function main() {
    lcov_isexits
    get_param $1 $2
}

main $1 $2
